package com.lmu.travel.controller;

import com.lmu.travel.entity.Category;
import com.lmu.travel.entity.Result;
import com.lmu.travel.service.CategoryService;
import com.lmu.travel.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxuhuang
 * @className: CategoryController
 * @description: 菜单栏接口
 * @email liuxuhuang3970@qq.com
 * @date 2021/6/28 16:58
 * @version：1.0
 */
@RestController
@RequestMapping(value = "category")
@Api("菜单栏接口")
public class CategoryController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("查询菜单栏所有选项")
    @GetMapping(value = "findAllCategory")
    public Result<Object> findAllCategory() {
        if (redisUtil.exists("categorys")) {
            List<Object> categories = redisUtil.getList("categorys");
            return new Result<Object>(200, true, categories, "查询菜单成功");
        } else {
            List<Category> categories = categoryService.list(null);
            List<Object> categoryList = new ArrayList();
            for (Category category : categories
            ) {
                categoryList.add(category.getCname());
            }
            redisUtil.setList("categorys", categoryList);
            return new Result<Object>(200, true, categoryList, "查询菜单成功");
        }

    }
}
